package com.sdy.supermarket.web.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sdy.common.model.BizException;
import com.sdy.common.model.Response;
import com.sdy.common.utils.StringUtil;
import com.sdy.fs.api.FsApi;
import com.sdy.supermarket.biz.model.GsBannerManage;
import com.sdy.supermarket.biz.service.GsBannerManageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.web.bind.annotation.*;
import com.sdy.mvc.controller.BaseController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;


/**
 * <p>
 * Banner管理
 * </p>
 *
 * @author zzq
 * @since 2019-07-30
 */
@Slf4j
@RestController
@RequestMapping("/gsBannerManage")
public class GsBannerManageController extends BaseController {
    @Autowired
    private GsBannerManageService gsBannerManageService;
    @Autowired
    private FsApi fsApi;

    @PostMapping("/save")
    public Response saveOrUpdateBannerManage(@RequestBody GsBannerManage gsBannerManage) throws BizException {
        return gsBannerManageService.saveBannerManage(gsBannerManage);
    }

    /**
     * 获取Banner数据
     *
     * @return
     */
    @GetMapping("/getOne")
    public Response getById() {
        List<GsBannerManage> list = gsBannerManageService.list(Wrappers.<GsBannerManage>lambdaQuery().orderByAsc(GsBannerManage::getId));
        if (list.size() > 0) {
            GsBannerManage gsBannerManage = list.get(0);
            if (StringUtil.isNotEmpty(gsBannerManage.getBannerImgUrl()) && StringUtil.isNotBlank(gsBannerManage.getBannerImgUrl())) {
                gsBannerManage.setFileInfo(fsApi.getFileInfo(gsBannerManage.getBannerImgUrl()));
            } else {
                gsBannerManage.setFileInfo(null);
            }
            return Response.success(gsBannerManage);
        }
        return Response.success();
    }

}
